//
//  AppDelegate.m
//  FNer
//
//  Created by dllo on 15/11/4.
//  Copyright © 2015年 刘志强. All rights reserved.
//

#import "AppDelegate.h"

#import "DataHandle.h"
#import "MyTabBar.h"
#import "RESideMenu.h"
#import "LeftViewController.h"
#import "MusicPlayerViewController.h"
@interface AppDelegate ()<RESideMenuDelegate>




@end

@implementation AppDelegate

static NSString *kUserHasOnboardedKey = @"user_has_onboarded";
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    
    [self.window makeKeyAndVisible];
    
    UIViewController* vc = [[UIViewController alloc] initWithNibName:nil bundle:nil];
    self.window.rootViewController = vc;

    //启动页
    NSString * userHasOnboared = [[NSUserDefaults standardUserDefaults] objectForKey:kUserHasOnboardedKey];
    if ([userHasOnboared isEqualToString:@"yes"]) {
        [self createAppView];
    }
    
    else {
        [[NSUserDefaults standardUserDefaults] setObject:@"no" forKey:kUserHasOnboardedKey];
        self.window.rootViewController = [self generateFirstDemoVC];
        
        [[NSUserDefaults standardUserDefaults] setObject:@"yes" forKey:kUserHasOnboardedKey];
        
    }
    application.statusBarStyle = UIStatusBarStyleLightContent;
    

    
    //更换主题

    if ([[[NSUserDefaults standardUserDefaults]objectForKey:@"isNight"]isEqualToString:@"YES"]) {

        SHAREDAPPDELE.window.alpha = 0.5;
    }
    else
    {

        SHAREDAPPDELE.window.alpha = 1.0;
    }

    
    return YES;
}



//开始创建
-(void)createAppView
{
    
    //创建并打开数据库
    [[DataHandle shareDataHandle] openDB];
    [[DataHandle shareDataHandle] createChannelTable];
    [[DataHandle shareDataHandle] createMusicTable];
    [[DataHandle shareDataHandle] createVideoTable];
    [[DataHandle shareDataHandle] createMovieTable];
    
    self.myTB = [MyTabBar shareMyTabBar];
      self.myTB.delegate = self;
    LeftViewController *left = [[LeftViewController alloc]init];
    RESideMenu *reside =[[RESideMenu alloc]initWithContentViewController:self.myTB leftMenuViewController:left rightMenuViewController:nil];
    reside.backgroundImage = [UIImage imageNamed:@"Stars"];
    reside.menuPreferredStatusBarStyle = 1; // UIStatusBarStyleLightContent
    reside.delegate = self;
    reside.contentViewShadowColor = [UIColor blackColor];
    reside.contentViewShadowOffset = CGSizeMake(0, 0);
    reside.contentViewShadowOpacity = 0.6;
    reside.contentViewShadowRadius = 12;
    reside.contentViewShadowEnabled = YES;

    
    self.window.rootViewController = reside;
    

}

//启动页
- (void)handleOnboardingCompletion {
    [self createAppView];
}

- (OnboardingViewController *)generateFirstDemoVC {
    
    OnboardingContentViewController *firstPage = [OnboardingContentViewController contentWithTitle:@"欢迎来到FNER" body:nil image:[UIImage imageNamed:@"1.jpg"]  buttonText:nil action:^{
    }];
    
    OnboardingContentViewController *secondPage = [OnboardingContentViewController contentWithTitle:@"更多视频" body:nil image:[UIImage imageNamed:@"2.jpg"] buttonText:nil action:^{
        
    }];

    OnboardingContentViewController *thirdPage = [OnboardingContentViewController contentWithTitle:@"更多音频" body:nil image:[UIImage imageNamed:@"3.jpg"] buttonText:nil action:^{
        
    }];
    
    OnboardingContentViewController *forthPage = [OnboardingContentViewController contentWithTitle:@"更多美图" body:@"" image:[UIImage imageNamed:@"4.jpg"] buttonText:@"" action:^{
        [self handleOnboardingCompletion];
    }];
    OnboardingViewController *onboardingVC = [OnboardingViewController onboardWithBackgroundImage:nil contents:@[firstPage, secondPage, thirdPage,forthPage]];
    onboardingVC.shouldFadeTransitions = YES;
    onboardingVC.fadePageControlOnLastPage = YES;
    onboardingVC.fadeSkipButtonOnLastPage = YES;
    onboardingVC.allowSkipping = YES;
    onboardingVC.skipHandler = ^{
        [self handleOnboardingCompletion];
    };
    
    return onboardingVC;
}



-(void)changeColor:(NSNotification *)notification
{
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:NO];
    
    NSString *str = [notification object];
    if ([str isEqualToString:@"no"]) {
        [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:NO];
    }
    //保存当前状态值
    [[NSUserDefaults standardUserDefaults] setObject:str forKey:@"night"];
}



- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
    MusicPlayerViewController *m =  [MusicPlayerViewController sharePlayerDataHandel];
    
    [m becomeFirstResponder];
    
    [m setLockScreenNowPlayingInfo];

    
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    [[UIApplication sharedApplication] endReceivingRemoteControlEvents];
    MusicPlayerViewController *m =  [MusicPlayerViewController sharePlayerDataHandel];
    //NSLog(@"%@",m);
    [m resignFirstResponder];

}


//添加远程监控
-(void)remoteControlReceivedWithEvent:(UIEvent *)event{
    
    
    
    
    
    MusicPlayerViewController *m =  [MusicPlayerViewController sharePlayerDataHandel];
    [m.pasueButton  setBackgroundImage:[UIImage imageNamed:@"play"] forState:(UIControlStateNormal)];
    if (event.type == UIEventTypeRemoteControl) {
        
        if (event.subtype == UIEventSubtypeRemoteControlPause){
            
            
            [m pauseAction:nil];
            
            
        } else if (event.subtype == UIEventSubtypeRemoteControlNextTrack){
            
            [m nextAction:nil];
            
            [m setLockScreenNowPlayingInfo];
            
            
        }
        else if (event.subtype == UIEventSubtypeRemoteControlPlay)
        {
            
            [m pauseAction:nil];
            
        }
        else if (event.subtype ==  UIEventSubtypeRemoteControlPreviousTrack)
        {
            [m lastAction:nil];
            [m setLockScreenNowPlayingInfo];
        }
        
    }
    
}




- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
